/**
 * 
 */
package systole.persistence.brokersDB;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import systole.domain.persons.Medic;
import systole.domain.persons.Person;
import systole.domain.persons.identityCard.IdentityCard;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.MedicBroker;
import systole.view.messages.ErrorMessages;

/**
 * @author jmj
 *
 */
public class MedicBrokerDB extends BrokerDB implements MedicBroker {

    /**
     *
     */
    public MedicBrokerDB() {
        super();
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.MedicBroker#getAllMedics()
     */
    @Override
    public List<Medic> getAllMedics() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting all medics");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            List<Medic> medics = currentSession.createQuery("from Medic m order by upper(m.surname)").list();
            this.logger.logDebug("getting medics successfully");
            return medics;
        } catch (HibernateException e) {
            this.logger.logError("error on get all medics, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los Médicos", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.MedicBroker#getMedicById(long)
     */
    @Override
    public Medic getMedicById(Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting medic instance");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Medic instance = (Medic) currentSession.get(Medic.class, id);
            this.logger.logDebug("get medic successfully");
            return instance;
        } catch (HibernateException e) {
            this.logger.logError("error on get medic by id, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener el Médico", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.MedicBroker#insert(systole.domain.persons.Medic)
     */
    @Override
    public void insert(Medic medic) throws ExceptionDAO {
        try {
            this.logger.logDebug("save medic ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(medic);
            this.logger.logDebug("save successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on save medic, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.MedicBroker#update(systole.domain.persons.Medic)
     */
    @Override
    public void update(Medic medic) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating medic ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(medic);
            this.logger.logDebug("update medic successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on update medic, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    @SuppressWarnings("unchecked")
    public boolean existCardIdentity(IdentityCard identityCard, Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("exist identity card");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Person.class).
                    add((id == null) ? Restrictions.eq("identityCard", identityCard)
                    : Restrictions.and(Restrictions.eq("identityCard", identityCard), Restrictions.ne("id", id)));
            List list = criteria.list();
            this.logger.logDebug("exist identity card successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist identity card, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo verificar el Tipo y Número de Documento", e.fillInStackTrace());
        }
    }
}
